<?php

namespace app\controller;

use app\BaseController;
use app\model\Permission;
use app\Request;
use app\validator\PermissionSave;

//  权限接口
class PermissionController extends BaseController{
    public function list(Request $request){
        $limit = $request->param('limit') ?? 5;
        $page = $request->param('page') ?? 1;
        $name = $request->param('name') ?? '';
        $field = 'id,name,title,status,path,icon';
        $order = 'name';
        $perm = new Permission();
        $data = $perm->_list($page,$limit,$name,$field,$order);
        return json($data,$data['code']);
    }

    public function tree(){
        $list = Permission::order(['name' => 'desc'])->select()->toArray();
        $tree = $this->buildTree($list);
        if($tree)
            return json([
                'code' => 200,
                'msg' => '获取成功',
                'data' => $tree
            ]);
        return json([
            'code' => 400,
            'msg' => '获取失败'
        ],400);
    }

    public function save(Request $request){
        $data = $request->param();

        $validate = new PermissionSave();
        if (isset($data['id']) && intval($data['id']) >= 0)
            $scene = 'update'; // 更新数据
        else
            $scene = 'create';  // 添加数据

        //  验证参数
        if (!$validate->scene($scene)->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $id =  $data['id'] ?? 0;
        $pid =  $data['pid'];
        $name = $data['name'];
        $title = $data['title'];
        $status = $data['status'];
        $path = $data['path'];
        $icon = $data['icon'];
        $level = $data['level'];
        $type = $data['type'];

        if ($id > 0) {
            // 更新数据
            $perm = new Permission();
            $fields = ['id','name','status','title','icon','path','parent_id','level','type'];
            $values = [$id,$name,$status,$title,$icon,$path,$pid,$level,$type];
            $data = $perm->_update($fields, $values);
            return json($data,$data['code']);
        } else {
            // 新增数据
            $perm = new Permission();
            $fields = ['name','status','title','icon','path','parent_id','level','type'];
            $values = [$name,$status,$title,$icon,$path,$pid,$level,$type];
            $data = $perm->_create($fields, $values);
            return json($data,$data['code']);
        }
    }

    public function delete(Request $request){
        $id = $request->param('id');
        $perm = new Permission();
        $data = $perm->_delete($id);
        return  json($data,$data['code']);
    }

    private function buildTree(array $list, $parentId = 0){
        $tree = [];

        foreach ($list as $item) {
            if ($item['parent_id'] == $parentId) {
                $children = $this->buildTree($list, $item['id']);
                if (!empty($children)) {
                    $item['children'] = $children;
                } else {
                    $item['children'] = [];
                }
                $tree[] = $item;
            }
        }

        return $tree;
    }
}